﻿/**
 * 快捷键插件
 */
(function(E, $){

if( typeof $.isEmptyObject !== "function" ){
	$.isEmptyObject = function(obj){
		if( !obj || typeof obj !== "object" ){
			return true;
		}
		
		for(var i in obj){
			return false;
		}
		return true;
	}
}

var Consts = E.consts, Event = E.mod('Event'),
	ctrlKeys = E.config('ctrlKeys'),
	ctrlShiftKeys = E.config('ctrlShiftKeys'),
	timer;

if( $.isEmptyObject(ctrlKeys) && $.isEmptyObject(ctrlShiftKeys) ){
	E.log('run', 'ctrlKeys && ctrlShiftKeys is empty');
	return;
}else{
	E.log('run', 'shortcutKey is binded');
}

// 2011-08-12 潘雪鹏
// 使用keydown比较合适一些，如使用keyup，
// 则必须保证松开按键时，先松开ctrl后松开字幕键（如Q），否则将得不到正确的keyCode

// 键盘按下事件，处理快捷键
Event.bind('keydown', function(dom, e){
	if( !e.ctrlKey ){
		return true;
	}
	var cmd = null,
		keyCode = e.keyCode || e.which || e.charCode;
		// key = String.fromCharCode(keyCode).toLowerCase();
	
	if (e.ctrlKey && !e.altKey) {
		cmd = e.shiftKey ? ctrlShiftKeys[keyCode] : ctrlKeys[keyCode];
	}
	
	// 阻止快捷键可能和浏览器或系统其他快捷键冲突
	//e.preventDefault ? e.preventDefault() : (e.returnValue = false);
	
	if ( cmd ) {
		
		var Plugin = E.plugin(cmd);
		
		if( Plugin){
			Plugin.isEnabled() && Plugin.click(null, Plugin)
			E.log('run', '执行快捷键命令'+cmd);
		}else{
			E.execCommand(cmd, null, null);
		}
		return false;
	}else if( keyCode <16 || keyCode >18 ){
		E.log('run', 'keyCode:'+keyCode);
	}
});

function onkeydown( cmd, keyCode ){

}

})(jQEditor, jQuery);